/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.wiki.security;

import foo.bar.wiki.domain.User;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;

import java.util.Set;
import java.util.HashSet;

/**
 * An extension to {@link SimpleAccount} allowing the account to be disabled.
 *
 * @author tmjee
 * @version $Date: 2009-03-14 14:53:07 +0800 (Sat, 14 Mar 2009) $ $Id$
 */
public class UserAccount extends SimpleAccount {

    private User user;
    private boolean disabled;

    public UserAccount(User user, String realmName) {
        super(new SimplePrincipalCollection(user, realmName), user.getPassword());
        this.user = user;
        setRoles(new UserRoleToJSecurityRoleAsStringTransformer().transform(user));

        Set<Permission> effectivePermissions = new HashSet<Permission>();
        effectivePermissions.addAll(new UserPermissionToJSecurityPermissionTransformer().transform(user));
        effectivePermissions.addAll(new UserRoleToJSecurityPermissionTransformer().transform(user));
        setObjectPermissions(effectivePermissions);

        setLocked(user.isLocked());
        setDisabled(user.isDisabled());
        setCredentialsExpired(user.isCredentialExpired());
    }

    public UserAccount(PrincipalCollection principals, Object credentials, Set<String> roleNames,
                       Set<Permission> permissions, boolean locked, boolean disabled, boolean credentialExpired) {
        super(principals, credentials, roleNames, permissions);
        this.disabled = disabled;
        setLocked(locked);
        setCredentialsExpired(credentialExpired);
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public User getUser() {
        return user;
    }

    public boolean isDisabled() {
        return disabled;
    }

}
